<?php
namespace App\Models;

use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Http\Controllers\Api\V1\Wechat\WechatController;

class UsersWechat extends Authenticatable  implements JWTSubject
{

    public $table = 'users_wechat';


    protected $hidden = ['openid', 'remember_token'];

    //累计消费
    public function getCountAttribute(){

    	$wechat_id = $this->attributes['id'];
    	$sum = Order::where('wechat_id',$wechat_id)->where('pay_status',Order::STATUS_SUCCESS)->sum('total_amount');
    	return $sum / 100;
    }

    //创建会员
    public static function VipCreate($wechat_id,$form_id){
    	$row = UsersWechat::where('type','>',0)->orderBy('type','DESC')->first();
        $vipNumber = empty($row) ?'A001': getNumber($row->number);//计算出编号
        $user =  UsersWechat::find($wechat_id);
		$user->number = $vipNumber;
		$user->type = empty($row) ? 1 : intval($row->type) + 1;
		$user->save();
        $people_number = SiteConfig::where('option_name', 'people_number')->value('option_value');
        if ($people_number > 1) {
            SiteConfig::where('option_name', 'people_number')->decrement('option_value');
        }

        //发送模版消息
        $WechatController = new WechatController();
        $param = [
            'form_id'=>$form_id,
            'wechat_id'=>$wechat_id,
            'type'=>1
        ];
        $WechatController->templateMessage($param);
		return 'Success';
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
    public function getAuthIdentifierName()
    {
        return 'id';
    }
    public function getAuthIdentifier()
    {

    }
    public function getAuthPassword()
    {

    }
    public function getRemenberToken()
    {

    }
    public function setRememberToken($value)
    {

    }
    public function getRememberTokenName()
    {

    }
}
